<?php

// Diary Widget 0.0.1 for the SilverStripe Diary Module
// 17.07.2010
// Author:  nivanka@silverstripers.com
// Save this and DiaryWidget.ss to widget_Diary/ and run "db/build".

class DiaryWidget extends Widget{
	
	static $title = "Diary events for this month";
	static $cmsTitle = "Diary Widget";
	static $description = "Shows a very generic Diary including all the events from each and every Diary.";
	
	/*
	 *Get the Diary Head
	 */
	 
	 function getHead(){
		$head ='	<tr class="calhead">';
		$head.='		<td>'. _t('DiaryHolder.SUN',"Sun") .'</td>';
		$head.='		<td>'. _t('DiaryHolder.MON',"Mon") .'</td>';
		$head.='		<td>'. _t('DiaryHolder.TUE',"Tue") .'</td>';
		$head.='		<td>'. _t('DiaryHolder.WED',"Wed") .'</td>';
		$head.='		<td>'. _t('DiaryHolder.THU',"Thu") .'</td>';
		$head.='		<td>'. _t('DiaryHolder.FRI',"Fri") .'</td>';
		$head.='		<td>'. _t('DiaryHolder.SAT',"Sat") .'</td>';
		$head.='	</tr>';
		
		return $head;
	 }
	
	/*
	 * Get the Diary for the selected month
	 * if the month is not selected then the Diary will be initilized for the surrent month
	 * the Diary is rendered using a table, the codes below creates the table
	 */
	function getEvents(){
		
		Requirements::css("diary/css/diarywidget.css");
		Requirements::css("diary/javascript/facebox/facebox.css");
		Requirements::javascript("diary/javascript/jquery.js");
		Requirements::javascript("diary/javascript/facebox/facebox.js");
		Requirements::javascript("diary/javascript/Diary.js");
		
		$calLink = DataObject::get_one("DiaryHolder")->URLSegment;
		
		// Check whether the month is selected or else the widget will get the current month
		$monthset = (int)Session::get("CalMonth");
		$today = getdate();
		if($monthset)
			$today = getdate($monthset);
		
		
		$firstday = getdate( mktime(0,0,0,$today['mon'],1,$today['year']) );
		$lastday = getdate( mktime(0,0,0,$today['mon'] + 1,0,$today['year']) );
		
		// from this line the Diary output is generated
		$table = "<tr>";
		
		$count = 0;
		$firstdaypass = 0;
		for($i = 0; $i < ($lastday['mday'] - $firstday['mday'] + 1); $i++){
		
			$thisday = getdate(mktime(0,0,0,$today['mon'],$i + 1,$today['year']));
			$count++;
			
			/*
			 * For the first day of the month this determines from where it should start
			 */
			if($firstdaypass == 0){
				$thisdayname = substr($thisday['weekday'],0,3);
				$firstdaypass = 1;
				
				// if the first day is a sunday
				if(strcmp($thisdayname,"Sun") == 0){
					$events =  $this->getEventsForDay($thisday);
					if($events){
						$table .= "<td class='active' onclick='openpopup(\"";
						foreach($events as $event){
							 $table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
						}
						$table .= "\")'>" . ($i + 1) . "</td>";
					}
					else
						$table .= "<td>" . ($i + 1) . "</td>";
					$count = 1;
				}
				// if the first day is a monday
				else if(strcmp($thisdayname,"Mon") == 0){
				
					$events =  $this->getEventsForDay($thisday);
					if($events){
						$table .= "<td></td><td class='active' onclick='openpopup(\"";
						foreach($events as $event){
							$table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
						}
						$table .= "\")'>" . ($i + 1) . "</td>";
					}
					else
						$table .= "<td></td><td>" . ($i + 1) . "</td>";
					$count = 2;
				}	
				// if the first day is a tuesday
				else if(strcmp($thisdayname,"Tue") == 0){
					$events =  $this->getEventsForDay($thisday);
					if($events){
						$table .= "<td></td><td></td><td class='active' onclick='openpopup(\"";
						foreach($events as $event){
							$table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
						}
						$table .= "\")'>" . ($i + 1) . "</td>";
					}
					else
						$table .= "<td></td><td></td><td>" . ($i + 1) . "</td>";
					$count = 3;
				}			
				// if the first day is a wednesday	
				else if(strcmp($thisdayname,"Wed") == 0){
					$events =  $this->getEventsForDay($thisday);
					if($events){
						$table .= "<td></td><td></td><td></td><td class='active' onclick='openpopup(\"";
						foreach($events as $event){
							$table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
						}
						$table .= "\")'>" . ($i + 1) . "</td>";
					}
					else
						$table .= "<td></td><td></td><td></td><td>" . ($i + 1) . "</td>";
					$count = 4;
				}
				// if the first day is a thursday
				else if(strcmp($thisdayname,"Thu") == 0){
					$events =  $this->getEventsForDay($thisday);
					if($events){
						$table .= "<td></td><td></td><td></td><td></td><td class='active' onclick='openpopup(\"";
						foreach($events as $event){
							$table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
						}
						$table .= "\")'>" . ($i + 1) . "</td>";
					}
					else
						$table .= "<td></td><td></td><td></td><td></td><td>" . ($i + 1) . "</td>";
					$count = 5;
				}
				// if the first day is a friday
				else if(strcmp($thisdayname,"Fri") == 0){
					$events =  $this->getEventsForDay($thisday);
					if($events){
						$table .= "<td></td><td></td><td></td><td></td><td></td><td class='active' onclick='openpopup(\"";
						foreach($events as $event){
							 $table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
						}
						$table .= "\")'>" . ($i + 1) . "</td>";
					}
					else
						$table .= "<td></td><td></td><td></td><td></td><td></td><td>" . ($i + 1) . "</td>";
					$count = 6;
				}
				// if the first day is a saturday
				else if(strcmp($thisdayname,"Sat") == 0){
					$events =  $this->getEventsForDay($thisday);
					if($events){
						$table .= "<td></td><td></td><td></td><td></td><td></td><td></td><td class='active' onclick='openpopup(\"";
						foreach($events as $event){
							 $table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
						}
						$table .= "\")'>" . ($i + 1) . "</td>";
					}
					else				
						$table .= "<td></td><td></td><td></td><td></td><td></td><td></td><td>" . ($i + 1) . "</td></tr><tr>";
					$count = 0;
				}
			} 
			//from here onwards the days are rendered normally. 
			//For each 7 day count (with the starting point) its adds a new table row
			else{
				$events =  $this->getEventsForDay($thisday);
				if($events){
					$table .= "<td class='active' onclick='openpopup(\"";
					foreach($events as $event){
						 $table .= "&lt;h3&gt;&lt;a href=\\\"" . $event->ViewLink() . "\\\" &gt;" . $event->Title . "&lt;/a&gt;&lt;/h3&gt;";
					}
					$table .= "\")'>" . ($i + 1) . "</td>";
				}
				else	
					$table .= "<td>" . ($i + 1) . "</td>";
				
				if($count == 7){
					$table .= "</tr><tr>";
					$count = 0;
				}
			}
		}
		$table .= "</tr>";
		return $table;
	}
	
	/*
	 * This function fetches the events for a specific date
	*/
	function getEventsForDay($thisday){
		$today_to_search = $thisday['year'] . "-" . $thisday['mon'] . "-" . $thisday['mday'];
		$events = DataObject::get("DiaryEvent","Start = '$today_to_search' OR End = '$today_to_search' OR '$today_to_search' BETWEEN Start AND End", "StartTime", "JOIN DiaryHolder_Events ON `DiaryEvent`.ID = DiaryHolder_Events.DiaryEventID");
		return $events;
	}
	
	/*
	 * Get the link to the previous month
	*/
	function getPreviousMonth(){
	
		$monthset = (int)Session::get("CalMonth");
		$today = getdate();
		
		if($monthset)
			$today = getdate($monthset);
		
		$page = Director::get_current_page();
		$lastmonth = getdate( mktime(0,0,0,$today['mon'] - 1 ,1) );
		$output = new DataObjectSet();
		$output->push(
			new ArrayData(
				array(
					"Link" => $page->Link() . "setMonth/?month=" . $lastmonth[0]."&amp;back=" . $page->URLSegment,
					"Name" => _t("DiaryHolder." . strtoupper($lastmonth['month']), $lastmonth['month']),
					"Title" => _t("DiaryHolder.PRE","Previous")
				)
			)
		);
		return $output;
		
	}
	
	/*
	 * Get the link to the next month
	*/
	function getNextMonth(){

		$monthset = (int)Session::get("CalMonth");
		$today = getdate();
		
		if($monthset)
			$today = getdate($monthset);
		
		$page = Director::get_current_page();
		$lastmonth = getdate( mktime(0,0,0,$today['mon'] + 1 ,1,$today['year']) );
		$output = new DataObjectSet();
		$output->push(
			new ArrayData(
				array(
					"Link" => $page->Link() . "/setMonth/?month=" . $lastmonth[0]."&amp;back=" . $page->URLSegment,
					"Name" => _t("DiaryHolder." . strtoupper($lastmonth['month']), $lastmonth['month']),
					"Title" => _t("DiaryHolder.NEXT","Next")
				)
			)
		);
		return $output;
	}
	
	/*
	 * Get the viewing month
	*/
	function CalDate(){
		$monthset = (int)Session::get("CalMonth");
		$today = getdate();
		if($monthset)
			$today = getdate($monthset);
		$date = _t("DiaryHolder." . strtoupper($today['month']), $today['month']) . " " . $today['year'];
		return $date;		
	}
}


